package com.incar.chuxing.utils.bos;

import com.baidubce.BceClientException;
import com.baidubce.BceServiceException;
import com.baidubce.auth.BceCredentials;
import com.baidubce.auth.DefaultBceCredentials;
import com.baidubce.auth.DefaultBceSessionCredentials;
import com.baidubce.services.bos.BosClient;
import com.baidubce.services.bos.BosClientConfiguration;
import com.baidubce.services.bos.model.PutObjectResponse;
import com.incar.chuxing.utils.Logger;
import com.incar.chuxing.utils.ThreadPool;
import com.incar.chuxing.utils.ToastUtils;

import java.io.File;

import static com.taobao.accs.ACCSManager.mContext;

/**
 * Created by Michael on 11/10.
 */

public class UploadPicUtil {

    private static UploadPicUtil instance;
    public OnFinishListener onFinishListener;

    public static UploadPicUtil getInstance() {
        if (instance == null) {
            instance = new UploadPicUtil();
        }
        return instance;
    }

    public UploadPicUtil() {
    }

    public void uploadPicToBos(final File file, String ak, String sk,
                               String endPoint, String stsToken, final String bucketName,
                               final String objectKey, final OnFinishListener onFinishListener) {
        // 1. get pic params from ui: file name, file location uri etc
        // 2. send params to app server and get sts, bucket name and region
        // 3. upload selected pic to bos with sts etc, which bos client needs
        Logger.d("上传 开始" + objectKey);

        this.onFinishListener = onFinishListener;

        if (file == null) {
            ToastUtils.makeText(mContext, "上传失败", ToastUtils.LENGTH_SHORT).show();
            return;
        }
        BceCredentials credentials = null;
        if (stsToken != null && !stsToken.equalsIgnoreCase("")) {
            credentials = new DefaultBceSessionCredentials(ak, sk, stsToken);
        } else {
            credentials = new DefaultBceCredentials(ak, sk);
        }

        BosClientConfiguration config = new BosClientConfiguration();
        config.setCredentials(credentials);
        //  config.setEndpoint(endPoint); //Bucket所在区域
        final BosClient client = new BosClient(config);
        ThreadPool.execute(() -> {
            try {
                //上传Object
                Logger.d("上传" + objectKey);
                PutObjectResponse putObjectFromFileResponse = client.putObject(bucketName, objectKey, file);
                System.out.println("Error ErrorType: " + "上传成功");
                onFinishListener.onSuccess();
            } catch (BceServiceException e) {
                System.out.println("Error ErrorCode: " + e.getErrorCode());
                System.out.println("Error RequestId: " + e.getRequestId());
                System.out.println("Error StatusCode: " + e.getStatusCode());
                System.out.println("Error Message: " + e.getMessage());
                System.out.println("Error ErrorType: " + e.getErrorType());
                // finished uploading file, send a message to inform ui
                onFinishListener.onFailure(e.getErrorMessage());
            } catch (BceClientException e) {
                e.printStackTrace();
                System.out.println("Error Message: " + e.getMessage());
                onFinishListener.onFailure(e.getMessage());
            }
        });

    }


    public interface OnFinishListener {
        void onSuccess();

        void onFailure(String failReason);
    }

    /*
     * Java文件操作 获取不带扩展名的文件名
     */
    public String getFileNameNoEx(File nameFile) {
        String filename = nameFile.getName();
        if ((filename != null) && (filename.length() > 0)) {
            int dot = filename.lastIndexOf('.');
            if ((dot > -1) && (dot < (filename.length()))) {
                return filename.substring(0, dot);
            }
        }
        return filename;
    }
}
